package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.DictItem;
import com.personal.portal.admin.service.DictItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典值控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "字典值", description = "字典值信息管理接口")
@RestController
@RequestMapping("/admin/dict-item")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class DictItemController {

    @Resource
    private DictItemService dictItemService;

    /**
     * 根据ID查询字典值
     */
    @Operation(summary = "根据ID查询字典值", description = "根据字典值ID查询详细信息")
    @GetMapping("/{id}")
    public Result<DictItem> queryById(
            @Parameter(description = "字典值ID", required = true) 
            @PathVariable Long id) {
        try {
            DictItem dictItem = dictItemService.queryById(id);
            if (dictItem != null) {
                return Result.success(dictItem);
            } else {
                return Result.error("字典值信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询字典值信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询字典值列表
     */
    @Operation(summary = "根据条件查询字典值列表", description = "根据字典名称等条件查询字典值信息")
    @PostMapping("/list")
    public Result<List<DictItem>> queryList(
            @Parameter(description = "字典值查询条件") 
            @RequestBody DictItem dictItem) {
        try {
            List<DictItem> dictItemList = dictItemService.queryList(dictItem);
            return Result.success(dictItemList);
        } catch (Exception e) {
            return Result.error("查询字典值列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询字典值
     */
    @Operation(summary = "分页查询字典值", description = "支持分页和条件查询的字典值信息")
    @PostMapping("/page")
    public Result<Result.PageResult<DictItem>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "字典值查询条件") 
            @RequestBody DictItem dictItem) {
        try {
            Result.PageResult<DictItem> pageResult = dictItemService.queryPage(pageNum, pageSize, dictItem);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询字典值失败: " + e.getMessage());
        }
    }

    /**
     * 根据分类ID查询字典值列表
     */
    @Operation(summary = "根据分类ID查询字典值列表", description = "根据字典分类ID查询对应的字典值列表")
    @GetMapping("/category/{categoryId}")
    public Result<List<DictItem>> queryByCategoryId(
            @Parameter(description = "字典分类ID", required = true) 
            @PathVariable Long categoryId) {
        try {
            List<DictItem> dictItemList = dictItemService.queryByCategoryId(categoryId);
            return Result.success(dictItemList);
        } catch (Exception e) {
            return Result.error("根据分类ID查询字典值列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据分类代码查询字典值列表
     */
    @Operation(summary = "根据分类代码查询字典值列表", description = "根据字典分类代码查询对应的字典值列表")
    @GetMapping("/category-code/{categoryCode}")
    public Result<List<DictItem>> queryByCategoryCode(
            @Parameter(description = "字典分类代码", required = true) 
            @PathVariable String categoryCode) {
        try {
            List<DictItem> dictItemList = dictItemService.queryByCategoryCode(categoryCode);
            return Result.success(dictItemList);
        } catch (Exception e) {
            return Result.error("根据分类代码查询字典值列表失败: " + e.getMessage());
        }
    }

    /**
     * 创建字典值
     */
    @Operation(summary = "创建字典值", description = "新增一条字典值信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "字典值信息", required = true) 
            @RequestBody DictItem dictItem) {
        try {
            int result = dictItemService.insert(dictItem);
            if (result > 0) {
                return Result.success("创建字典值成功", dictItem.getId());
            } else {
                return Result.error("创建字典值失败");
            }
        } catch (Exception e) {
            return Result.error("创建字典值失败: " + e.getMessage());
        }
    }

    /**
     * 更新字典值
     */
    @Operation(summary = "更新字典值", description = "更新指定ID的字典值信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "字典值ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "字典值信息", required = true) 
            @RequestBody DictItem dictItem) {
        try {
            dictItem.setId(id);
            int result = dictItemService.update(dictItem);
            if (result > 0) {
                return Result.success("更新字典值成功", id);
            } else {
                return Result.error("更新字典值失败");
            }
        } catch (Exception e) {
            return Result.error("更新字典值失败: " + e.getMessage());
        }
    }

    /**
     * 删除字典值
     */
    @Operation(summary = "删除字典值", description = "根据ID删除指定的字典值信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "字典值ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = dictItemService.deleteById(id);
            if (result > 0) {
                return Result.success("删除字典值成功", id);
            } else {
                return Result.error("删除字典值失败");
            }
        } catch (Exception e) {
            return Result.error("删除字典值失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除字典值
     */
    @Operation(summary = "批量删除字典值", description = "根据ID列表批量删除字典值信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "字典值ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = dictItemService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除字典值失败: " + e.getMessage());
        }
    }
}